package com.alogic.vfs.xscript;

import com.alogic.vfs.core.VirtualFileSystem;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.BaseException;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扫描文件
 *
 * @since  1.6.13.37 [20200223 duanyy] <br>
 */
public class FileScan extends Segment {
    protected String pid = "$vfs";
    protected String $path = "/";
    protected String $pattern = "[\\S]*";
    protected String $offset = "0";
    protected String $limit = "100";
    protected String id;

    public FileScan(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);

        pid = PropertiesConstants.getString(p,"pid",pid,true);
        $path = PropertiesConstants.getRaw(p,"path",$path);
        $pattern = PropertiesConstants.getRaw(p, "pattern", $pattern);
        $offset = PropertiesConstants.getRaw(p, "offset", $offset);
        $limit = PropertiesConstants.getRaw(p, "limit", $limit);
        id = PropertiesConstants.getString(p,"id","$" + this.getXmlTag(),true);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx,
                             ExecuteWatcher watcher) {
        VirtualFileSystem vfs = ctx.getObject(pid);
        if (vfs == null){
            throw new BaseException("core.e1001",String.format("Can not find vfs:%s", pid));
        }

        String path = PropertiesConstants.transform(ctx,$path,"/");
        String pattern = PropertiesConstants.transform(ctx,$pattern,"[\\S]*");
        int offset = PropertiesConstants.getInt(ctx,$offset,0);
        int limit = PropertiesConstants.getInt(ctx,$limit,100);

        Map<String,Object> result = new HashMap<String,Object>();
        vfs.listFiles(path, pattern, result, offset, limit);

        Object file = result.get("file");
        int fileCount = 0;
        if (file != null && file instanceof List<?>){
            List<Object> fileList = (List<Object>)file;
            for (Object obj:fileList){
                if (obj instanceof Map<?,?>){
                    Map<String,Object> item = (Map<String,Object>)obj;
                    String name = JsonTools.getString(item,"name","");
                    String length = JsonTools.getString(item,"length","0");
                    String lastModified = JsonTools.getString(item,"lastModified","0");
                    String permission = JsonTools.getString(item,"permission","0");
                    String filePath = JsonTools.getString(item,"path","");
                    String fileDir = JsonTools.getString(item,"dir","");
                    if (StringUtils.isNotEmpty(name)) {
                        ctx.SetValue("$filename", name);
                        ctx.SetValue("$fileLength",length);
                        ctx.SetValue("$fileLastModified",lastModified);
                        ctx.SetValue("$filePermission",permission);
                        ctx.SetValue("$filePath",filePath);
                        ctx.SetValue("$fileDir",fileDir);
                        fileCount ++;
                        super.onExecute(root,current,ctx,watcher);
                    }
                }
            }
        }
        ctx.SetValue(id,String.valueOf(fileCount));
    }
}